/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import entities.CuotasACobrar;
import entities.Pagos;
import entities.VentasCabecera;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

/**
 *
 * @author edgar
 */
@ManagedBean(name = "pagosControlador")
@SessionScoped
public class PagosController {

    @PersistenceContext(unitName = "sgal3PU")
  //  EntityManager em;
    private Pagos p;

   

    public void pagar(CuotasACobrar cc, Date fecha, VentasCabecera vc, EntityManager em) {
        try {
            UserTransaction t = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
            t.begin();
            p=new Pagos();
            p.setCuotasACobrar(cc);
            p.setFecha(fecha);
            p.setMonto(cc.getMonto());
            p.setCodCliente(vc.getCodCliente());
            em.persist(p);
            new CuotasACobrarController().cancelarCuota(cc, em);
            t.commit();
        } catch (NamingException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotSupportedException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicMixedException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicRollbackException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(PagosController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the p
     */
    public Pagos getP() {
        return p;
    }

    /**
     * @param p the p to set
     */
    public void setP(Pagos p) {
        this.p = p;
    }
}
